
    var title = "How to Structure a LaTeX Document";
    var theme = "dark_blue";
    var current_slide_number = 1 ; 
    var TOTAL_SLIDES = 0;
    var slideshow_started = 0; 
    var myDate=new Date();
    var t_old = myDate.getTime();
    var animation_mode = 3;
    var time_data = null;
    var presentation_mode_index = null ;
    var time_id = null;
    var local_timing_xml_ob = null ;
    
    function fun_update_timing_dom(){
    	myDate = new Date();
    	var t_new = myDate.getTime() ;
    	var t_diff = t_new - t_old ;
    	t_old = t_new ;

    	$("#time_diff").html("Last SlideDuration is = " + t_diff );

    	
    	new_el=local_timing_xml_ob.createElement("slide");
    	new_el.setAttribute("number",current_slide_number);
    	new_el.setAttribute("duration",t_diff);
    	$("slideshow",local_timing_xml_ob).append(new_el);

    	$("#timedom").append("<li> slide : " + current_slide_number + " = " + t_diff + "</li>" );
    	
    	
    	//alert((new XMLSerializer()).serializeToString(local_timing_xml_ob));
    }

    function fun_edit_mode(){
		$("#html").hide();
		$("#butt1").hide();
		$("#control-panel").hide();
		
		$("#wiki").show();
		$("#butt").show();
    }
    function fun_present_mode(){
    	$("#wiki").hide();
		$(".slide").hide();
    	$("#butt").hide();
    	$("#toc").slideUp();
		$("#butt1").show();
		$("#html").show();
		$("#control-panel").show();
		
    }
   
    function fun_goto_slide(num){
    	if(num <= TOTAL_SLIDES && num >= 1 ){
			fun_update_timing_dom();
			//alert( $("#slide"+current_slide_number+"").attr("z-index"));
	    	switch(animation_mode){
	    	case 1 : 
	    		$("#slide"+current_slide_number+"").hide(400);
				current_slide_number = num ;
				$("#slide"+num+"").show(400);
				
		    	break;
		    case 2 : 
		    	$("#slide"+current_slide_number+"").slideUp();
				current_slide_number = num ;
				$("#slide"+num+"").slideDown();

				break;
		    case 3 : 
		    	$("#slide"+current_slide_number+"").fadeOut(100,function(){ 
    	    		$("#slide"+num+"").fadeIn(100);
    			});
				current_slide_number = num ;
					break;
		    case 4 : 
		    	$("#slide"+current_slide_number+"").fadeOut(200 ,function(){ 
    	    		$("#slide"+num+"").fadeIn(200);
    			});
				current_slide_number = num ;
				break;
	    	default : 
	    		
	    		$("#slide"+current_slide_number+"").hide();
				current_slide_number = num ;
				$("#slide"+num+"").show();
			
			break;
	    	}//switch
	    	
			
		}else{
		alert("not allowed");
		}
    }

    function fun_goto_slide1(num){
    	if(num <= TOTAL_SLIDES && num >= 1 ){
			
			//alert( $("#slide"+current_slide_number+"").attr("z-index"));
	    	switch(animation_mode){
	    	case 1 : 
	    		$("#slide"+current_slide_number+"").hide(400);
				current_slide_number = num ;
				$("#slide"+num+"").show(400);
				
		    	break;
		    case 2 : 
		    	$("#slide"+current_slide_number+"").slideUp();
				current_slide_number = num ;
				$("#slide"+num+"").slideDown();

				break;
		    case 3 : 
		    	$("#slide"+current_slide_number+"").fadeOut(100,function(){ 
    	    		$("#slide"+num+"").fadeIn(100);
    			});
				current_slide_number = num ;
					break;
		    case 4 : 
		    	$("#slide"+current_slide_number+"").fadeOut(200 ,function(){ 
    	    		$("#slide"+num+"").fadeIn(200);
    			});
				current_slide_number = num ;
				break;
	    	default : 
	    		
	    		$("#slide"+current_slide_number+"").hide();
				current_slide_number = num ;
				$("#slide"+num+"").show();
			
			break;
	    	}//switch
	    	
			
		}else{
		alert("not allowed");
		}
    }
    
    function hide_toc(){
		$("#toc").slideUp();
    }

	
    function fun_update_slideshow(but_id){

		if(slideshow_started){
	    	switch (but_id){
	    	case "html": 
	    	case "next_slide": fun_goto_slide(current_slide_number + 1) ; break;
	    	case "prev_slide": fun_goto_slide(current_slide_number - 1) ; break;
	    	case "on_off": // stop slideshow
	    		slideshow_started = 0;
				$(".slide").hide();
				$("#on_off").attr("value","Start");
				fun_update_timing_dom();
				
	    	break;
	    	default : alert("I\'m sure it was great");
	    	}
	    	
		}else{

			switch (but_id){
	    	case "on_off": // start slideshow
		    	$(".slide").hide();
				TOTAL_SLIDES = $(".slide").length ;
				t_old = myDate.getTime();
				$("#slide1").show();
				current_slide_number = 1;
				slideshow_started = 1;
				$("#on_off").attr("value","Stop");
				$.get("empty_timing.xml", {}, function(data){
					local_timing_xml_ob = data;
				});

			break;
	    	default : 
			alert("first click on start");
			}//SWITCH
		}//ELSE

	}//end of function
    
    
$(document).ready(function (){

	//load data into textarea
	$.get("get_wiki_txt.php", {}, function(data_txt){
		$("#wiki").html(data_txt);
	});

	$("#control-panel").hide();
	$("#butt1").hide();
	$("#butt").click(function(){
		$("#html").html(document.getElementById('wiki').value.wiki2html() ) ;
		$("#html").append("</div>") ;

		fun_present_mode();
		topic_depth = 0;
		chapter_index = 0 ; 
			$("#toc").html(toc_html);
			
			$(".slide h1").addClass("heading");
			
			toc_html = "";
			
	});
	
	$("#butt1").click(function(){
			fun_edit_mode();
	});

	$(".my_butt").click(function(){
			fun_update_slideshow($(this).attr("id"));
	});

	$("#toc_display").click(function (){
			$("#toc").slideDown(); 
	});

	$("#time_xml_post").click(function (){
		$.post("test.php", { timingdotxml : (new XMLSerializer()).serializeToString(local_timing_xml_ob)} , function(data){alert("Sent !!");} , "xml");
	});

	$("#Replay").click(function (){
	        //get timing.xml
		$(".slide").hide();
	    $.get("timing.xml", {}, function(data){
		        time_data= data;
		        $(".slide").hide();
				TOTAL_SLIDES = $(".slide").length ;
				$("#slide1").show();
				slideshow_started = 1 ;
		        presentation_mode_index = 0 ;
		        fun_replay_mode();
	    });
    });  
});

function fun_replay_mode(){
		//presentation_mode_index do not indicate slide number , it show slide element, which can be any slidenumber
	if(presentation_mode_index < $("slide", time_data).length){

		duration = parseFloat($("slide", time_data).eq(presentation_mode_index).attr("duration"));
		slide_num = parseFloat($("slide", time_data).eq(presentation_mode_index).attr("number"));
    	fun_goto_slide1(slide_num);
    	time_id = window.setTimeout("fun_replay_mode();",duration);
    	presentation_mode_index = presentation_mode_index + 1 ;
    	$("#time_diff").html("Slide " + slide_num + " will stay for " + duration + "  ms" );
	}
	else{
		slideshow_started = 0 ;
		presentation_mode_index = 0 ;
		alert("end of presentaion");
	}
}//func_end
	
